#include "global.h"
#include "Controller.h"
#include "Font2D.h"

bool finish = false;

void init(void)
{
	glClearColor(0.0, 0.0, 0.0, 0.0);
}

void reshape_func(int w, int h)
{
	glViewport(0, 0, (GLsizei)w, (GLsizei)h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(40.0, (GLfloat)w / (GLfloat)h, 5.0, 100.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

void display_func(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
	glPushAttrib(GL_ALL_ATTRIB_BITS);
	glDisable(GL_LIGHTING);
	glDisable(GL_FOG);
	glDisable(GL_DEPTH_TEST);
	glColor3f(1.0, 1.0, 1.0);
	Font2D::tip("Loading...");
	glPopAttrib();
	glutSwapBuffers();
}

void idle_func()
{
	Controller::get_instance().init();
	glutIdleFunc(NULL);
	finish = true;
	glutPostRedisplay();
}

void timer_func(int state)
{
	if (finish)
		Controller::get_instance().title_begin();
	else
		glutTimerFunc(state, timer_func, state);

	glutPostRedisplay();
}


int main(int argc, char** argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | GLUT_MULTISAMPLE);
	glutInitWindowSize(500, 500);
	glutInitWindowPosition(100, 100);
	glutCreateWindow (argv[0]);
	
	if (glewInit() != GLEW_OK)
		exit(0);

	init();
	
	glutReshapeFunc(reshape_func);
	glutDisplayFunc(display_func);
	glutIdleFunc(idle_func);
	glutTimerFunc(1000, timer_func, 500);
	//glutMainLoop();

	glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION);
	glutMainLoop();
	
	Controller::get_instance().destory();
	return 0;

}